|
In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and a type variable a , and means that a can only be instantiated to a type whose members support the overloaded operations associated with T .Type classes first appeared in the Haskell programming language,〔"Type classes, first proposed during the design of the Haskell programming language, ..." —(John Garrett Morris (2013), "Type Classes and Instance Chains: A Relational Approach" )〕 and were originally conceived as a way of implementing overloaded arithmetic and equality operators in a principled fashion. In contrast with the "eqtypes" of Standard ML, overloading the equality operator through the use of type classes in Haskell does not require extensive modification of the compiler frontend or the underlying type system. Since their creation, many other applications of type classes have been discovered. == Overview == The programmer defines a type class by specifying a set of function or constant names, together with their respective types, that must exist for every type that belongs to the class. In Haskell, types can be parameterized; a type class Eq intended to contain types that admit equality would be declared in the following way:class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool This declaration may be read as stating a "type a belongs to type class Eq if there are functions named (==) , and (/=) , of the appropriate types, defined on it." A programmer could then define a function elem (which determines if an element is in a list) in the following way:elem :: (Eq a) => a -> () -> Bool elem y , that is, lists of elements of type t .Note that type classes are different from classes in object-oriented programming languages. In particular, Eq is not a type: there is no such thing as a ''value'' of type Eq .Type classes are closely related to parametric polymorphism. For example, note that the type of elem as specified above would be the parametrically polymorphic type a -> () -> Bool were it not for the type class constraint "(Eq a) => ".抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Type class」の詳細全文を読む スポンサード リンク
|